package com.ws.universal.tools.utils.glide;

import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.ws.universal.tools.ContextInit;
import com.ws.universal.tools.utils.LogcatUtils;
import com.ws.universal.tools.utils.ToastUtils;

import java.io.File;

/**
 * Created by Administrator on 2018/6/4.
 */

public class GlideUtils {
    public static final String ANDROID_RESOURCE = "android.resource://";
    public static final String SEPARATOR = "/";

    private static int deferror;
    private static int def;


    //    /******加载网络图片******/
    //    public static void setImageUrl(String filePath, ImageView make_photo, int defaultDrawble) {
    //        Glide.with(ContextInit.getContext()).load(filePath).apply(RequestOptionUtils.getRequestOption(defaultDrawble)).into(make_photo);
    //    }
    //
    //    public static void setImageUrl(String filePath, ImageView make_photo) {
    //        Glide.with(ContextInit.getContext()).load(filePath).apply(RequestOptionUtils.getRequestOption()).into(make_photo);
    //    }
    //
    //    /******加载圆形图片******/
    //    public static void CreateImageCircular(String filePath, ImageView make_photo, int defaultImage) {
    //        Glide.with(ContextInit.getContext()).load(filePath).apply(RequestOptionUtils.getRequestOptionCircle(defaultImage)).into(make_photo);
    //    }
    //
    //    /******加载圆角图片 系统自带ImageView 使用的时候 刷新ui时 图片会闪烁 要自己做 数据 处理 例如 在请求接口时 判断数据是否和上次的请求结果相同 相同不做任何处理******/
    //    public static void CreateImageRound(String filePath, ImageView make_photo, int radius) {
    //        Glide.with(ContextInit.getContext()).load(filePath).apply(RequestOptionUtils.getCircleTransformRound()).into(make_photo);
    //
    //    }
    private static int head_error;
    private static int head_default;

    // 将资源ID转为Uri
    public static Uri resourceIdToUri(int resourceId) {
        return Uri.parse(ANDROID_RESOURCE + ContextInit.getContext().getPackageName() + SEPARATOR + resourceId);
    }

    // 将资源ID转为Uri
    public static void loadFile(String resourceId, ImageView imageView) {
        Glide.with(ContextInit.getContext()).load(new File(resourceId)).into(imageView);
    }

    // 加载drawable图片
    public static void loadResImage(int resId, ImageView imageView) {
        Glide.with(ContextInit.getContext()).load(resourceIdToUri(resId)).into(imageView);
    }

    /******加载圆角图片 完美解决 ui刷新时 图片闪烁 问题 ps 需要 自定义 圆形或者圆角 ImageView******/
    public static void CreateImageRound(String filePath, ImageView make_photo) {

        CreateImageRound(filePath, make_photo, deferror, def);

    }

    /******加载圆角图片 完美解决 ui刷新时 图片闪烁 问题 ps 需要 自定义 圆形或者圆角 ImageView******/
    public static void loadHeadImage(String filePath, ImageView make_photo) {

        CreateImageRound(filePath, make_photo, head_error, head_default);

    }

    public static void CreateImageRound(String filePath, ImageView make_photo, int imgLoadError, int imgPlaceho) {
        Glide.with(ContextInit.getContext())
                .load(filePath)
                .apply(RequestOptionUtils.getRequestOption(imgLoadError, imgPlaceho))
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {

                        if (e != null) {
                            // 输出加载失败的日志信息
                            LogcatUtils.e(" CreateImageRound onLoadFailed getMessage " + e.getMessage());
                            LogcatUtils.e(" CreateImageRound onLoadFailed e " + e);
                            ToastUtils.showLong("Image Loading Failed " + e.getMessage(), 10000);
                        }

                        return true;//表示中止Glide的默认错误处理逻辑
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        LogcatUtils.e(" Create ImageRound onResource Ready ");
                        return false;
                    }
                })
                .diskCacheStrategy(DiskCacheStrategy.ALL)  // 使用磁盘缓存 //默认
                .into(make_photo);
    }

    public static void blackWhiteTransformation(String filePath, ImageView make_photo, int imgLoadError, int imgPlaceho) {

        Glide.with(ContextInit.getContext()).load(filePath).apply(RequestOptionUtils.getBlackWhiteTransformation(imgLoadError, imgPlaceho)).into(make_photo);
    }

    public void setDeferror(int deferror) {
        this.deferror = deferror;
    }

    public void setDef(int def) {
        this.def = def;
    }

    public void setHead_error(int head_error) {
        this.head_error = head_error;
    }

    public void setHead_default(int head_default) {
        this.head_default = head_default;
    }
}
